package ewms.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelWriter;
import ewms.bo.EwmsWasteFlowInfoQueryBo;
import ewms.bo.EwmsReportQueryBo;
import ewms.service.IEwmsReportService;
import ewms.vo.EwmsEntryWarehouseVo;
import ewms.vo.EwmsReportVo;
import ewms.common.annotation.Log;
import ewms.common.core.domain.AjaxResult;
import ewms.common.core.page.TableDataInfo;
import ewms.common.enums.BusinessType;
import ewms.common.exception.CustomException;
import ewms.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 报表控制器类
 */
@Api(value = "危废报表控制器", tags = {"危废报表管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/business/report")
@Slf4j
public class EwmsReportController {

	@Autowired
	private IEwmsReportService iEwmsReportService;

	public EwmsReportController(IEwmsReportService iEwmsReportService) {
		this.iEwmsReportService = iEwmsReportService;
	}

	/**
	 * 获取年月列表
	 * @return
	 */
	@ApiOperation("获取年月列表")
	@GetMapping("/yearMonthList")
	public List<String> getYearMonthList(){
		return iEwmsReportService.selectYearMonthList();
	}

	/**
	 * 危废全流程报表
	 *
	 * @return
	 */
	@ApiOperation("危废全流程报表")
	@PreAuthorize("@ss.hasPermi('business:transferWarehouse:list')")
	@GetMapping("/wasteFlowInfoList")
	public TableDataInfo<EwmsReportVo.WasteFlowInfoVo> wasteFlowInfoList(@Validated EwmsWasteFlowInfoQueryBo bo) {
		return iEwmsReportService.selectWasteFlowInfoList(bo);
	}

	/**
	 * 导出危废全流程报表
	 */
	@ApiOperation("导出危废入库日报表列表")
	@PreAuthorize("@ss.hasPermi('business:entryWarehouse:export')")
	@Log(title = "危废入库日报表", businessType = BusinessType.EXPORT)
	@GetMapping("/wasteFlowInfoExport")
	public AjaxResult<EwmsEntryWarehouseVo> wasteFlowInfoExport(@Validated EwmsWasteFlowInfoQueryBo bo) {
		TableDataInfo<EwmsReportVo.WasteFlowInfoVo> page = iEwmsReportService.selectWasteFlowInfoList(bo);
		ExcelUtil<EwmsReportVo.WasteFlowInfoVo> util = new ExcelUtil<>(EwmsReportVo.WasteFlowInfoVo.class);
		return util.exportExcel(page.getRows(), "危废入库日报表");
	}

	/**
	 * 危废入库日报表列表
	 *
	 * @return
	 */
	@ApiOperation("危废入库日报表列表")
	@PreAuthorize("@ss.hasPermi('business:transferWarehouse:list')")
	@GetMapping("/wasteEntryWarehouseList")
	public List<EwmsReportVo.EntryWarehouseReportVo> selectWasteEntryWarehouseList(@Validated EwmsReportQueryBo bo) {
		return iEwmsReportService.selectWasteEntryWarehouseList(bo);
	}

	/**
	 * 导出危废入库日报表列表
	 */
	@ApiOperation("导出危废入库日报表列表")
	@PreAuthorize("@ss.hasPermi('business:entryWarehouse:export')")
	@Log(title = "危废入库日报表", businessType = BusinessType.EXPORT)
	@GetMapping("/wasteEntryWarehouseExport")
	public AjaxResult<EwmsEntryWarehouseVo> wasteEntryWarehouseExport(@Validated EwmsReportQueryBo bo) {
		List<EwmsReportVo.EntryWarehouseReportVo> list = iEwmsReportService.selectWasteEntryWarehouseList(bo);
		ExcelUtil<EwmsReportVo.EntryWarehouseReportVo> util = new ExcelUtil<>(EwmsReportVo.EntryWarehouseReportVo.class);
		return util.exportExcel(list, "危废入库日报表");
	}

	/**
	 * 危废出库日报
	 *
	 * @return
	 */
	@ApiOperation("危废出库日报表")
	@PreAuthorize("@ss.hasPermi('business:transferWarehouse:list')")
	@GetMapping("/wasteOutWarehouseList")
	public List<EwmsReportVo.OutWarehouseReportVo> selectWasteOutWarehouseList(@Validated EwmsReportQueryBo bo) {
		return iEwmsReportService.selectWasteOutWarehouseList(bo);
	}

	/**
	 * 导出危废出库日报表列表
	 */
	@ApiOperation("导出危废出库日报表列表")
	@PreAuthorize("@ss.hasPermi('business:entryWarehouse:export')")
	@Log(title = "危废出库日报表", businessType = BusinessType.EXPORT)
	@GetMapping("/wasteOutWarehouseExport")
	public AjaxResult<EwmsEntryWarehouseVo> wasteOutWarehouseExport(@Validated EwmsReportQueryBo bo) {
		List<EwmsReportVo.OutWarehouseReportVo> list = iEwmsReportService.selectWasteOutWarehouseList(bo);
		ExcelUtil<EwmsReportVo.OutWarehouseReportVo> util = new ExcelUtil<EwmsReportVo.OutWarehouseReportVo>(EwmsReportVo.OutWarehouseReportVo.class);
		return util.exportExcel(list, "危废出库日报表");
	}

	/**
	 * 危废出入库月报表
	 *
	 * @return
	 */
	@ApiOperation("危废出入库月报表")
	@PreAuthorize("@ss.hasPermi('business:transferWarehouse:list')")
	@GetMapping("/entryAndOutWarehouseMothReport")
	public List<EwmsReportVo.EntryAndOutWarehouseMonthReportVo> selectEntryAndOutWarehouseMothReport(@Validated EwmsReportQueryBo bo) {
		return iEwmsReportService.selectEntryAndOutWarehouseMothReport(bo);
	}

	/**
	 * 导出危废出入库月报表
	 */
	@ApiOperation("导出危废出入库月报表")
	@PreAuthorize("@ss.hasPermi('business:entryWarehouse:export')")
	@Log(title = "导出危废出入库月报表", businessType = BusinessType.EXPORT)
	@GetMapping("/entryAndOutWarehouseMothReportExport")
	public AjaxResult<EwmsEntryWarehouseVo> entryAndOutWarehouseMothReportExport(@Validated EwmsReportQueryBo bo) {
		List<EwmsReportVo.EntryAndOutWarehouseMonthReportVo> list = iEwmsReportService.selectEntryAndOutWarehouseMothReport(bo);
		ExcelUtil<EwmsReportVo.EntryAndOutWarehouseMonthReportVo> util = new ExcelUtil<EwmsReportVo.EntryAndOutWarehouseMonthReportVo>(EwmsReportVo.EntryAndOutWarehouseMonthReportVo.class);
		return util.exportExcel(list, "危废出库日报表");
	}

	/**
	 * 危废委外处置报表
	 *
	 * @return
	 */
	@ApiOperation("危废委外处置报表")
	@PreAuthorize("@ss.hasPermi('business:transferWarehouse:list')")
	@GetMapping("/outFactoryReport")
	public List<EwmsReportVo.WasteOutFactoryReportVo> outFactoryReport(@Validated EwmsReportQueryBo bo) {
		return iEwmsReportService.selectOutFactoryReport(bo);
	}

	/**
	 * 导出危废委外处置报表
	 */
	@ApiOperation("导出危废委外处置报表")
	@PreAuthorize("@ss.hasPermi('business:entryWarehouse:export')")
	@Log(title = "导出危废委外处置报表", businessType = BusinessType.EXPORT)
	@GetMapping("/outFactoryReportExport")
	public AjaxResult<EwmsEntryWarehouseVo> outFactoryReportExport(@Validated EwmsReportQueryBo bo) {
		List<EwmsReportVo.WasteOutFactoryReportVo> list = iEwmsReportService.selectOutFactoryReport(bo);
		ExcelUtil<EwmsReportVo.WasteOutFactoryReportVo> util = new ExcelUtil<EwmsReportVo.WasteOutFactoryReportVo>(EwmsReportVo.WasteOutFactoryReportVo.class);
		return util.exportExcel(list, "危废委外处置报表");
	}

	/**
	 * 危废焚化处置报表
	 *
	 * @return
	 */
	@ApiOperation("危废委外处置报表")
	@PreAuthorize("@ss.hasPermi('business:transferWarehouse:list')")
	@GetMapping("/incinerateHandleReport")
	public Map<String, Object> incinerateHandleReport(@Validated EwmsReportQueryBo bo) {
		return iEwmsReportService.selectIncinerateHandleReport(bo);
	}

	/**
	 * 导出危废焚化处置报表
	 *
	 * @return
	 */
	@ApiOperation("导出危废焚化处置报表")
	@PreAuthorize("@ss.hasPermi('business:entryWarehouse:export')")
	@Log(title = "导出焚化处置报表", businessType = BusinessType.EXPORT)
	@GetMapping("/incinerateHandleReportExport")
	public AjaxResult<Void> incinerateHandleReportExport(@Validated EwmsReportQueryBo bo) {
		Map<String, Object> resultMap = iEwmsReportService.selectIncinerateHandleReport(bo);
		List<Map> dataList = (List<Map>) resultMap.get("dataList");
		List<EwmsReportVo.WasteIncinerateHandleWasteInfo> wasteList = (List<EwmsReportVo.WasteIncinerateHandleWasteInfo>) resultMap.get("wasteList");

		OutputStream out = null;
		ExcelWriter wb = null;
		try {
			if (CollectionUtil.isNotEmpty(dataList) && CollectionUtil.isNotEmpty(wasteList)) {
				// 准备数据
				List<String> headerRow = wasteList.stream().map(waste -> waste.getWasteName()).collect(Collectors.toList());
				headerRow.add(0, "日期");
				List<List> convertDataList = new ArrayList<>();
				convertDataList.add(headerRow);
				for (Map<String, Object> map : dataList) {
					List<Object> dataRow = map.entrySet().stream().map(entry -> entry.getValue()).collect(Collectors.toList());
					convertDataList.add(dataRow);
				}
				// 通过工具类创建writer，默认创建xls格式
				wb = cn.hutool.poi.excel.ExcelUtil.getWriter();
				//创建xlsx格式的
				//ExcelWriter writer = ExcelUtil.getWriter(true);
				// 一次性写出内容，使用默认样式，强制输出标题
				wb.write(convertDataList, true);
				String filename = ExcelUtil.encodingFilename("危废焚化处置报表");
				//out为OutputStream，需要写出到的目标流
				out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
				wb.flush(out);
				return AjaxResult.success(filename);
			} else {
				throw new CustomException("报表数据未获取到");
			}
		} catch (FileNotFoundException e) {
			log.error("导出Excel异常{}", e.getMessage());
			throw new CustomException("导出Excel失败，请联系网站管理员！");
		} finally {
			if (wb != null) {
				wb.close();
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}
